package com.antwork.web.common;

import com.antwork.common.entity.BaseEntity;
import com.antwork.common.entity.Pagination;
import com.antwork.common.util.JSONHelper;
import com.opensymphony.xwork2.ActionSupport;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware, ServletContextAware {
	private static final long serialVersionUID = 7620009925942346125L;
	protected static Logger logger = LoggerFactory.getLogger(BaseAction.class);
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected SessionMap session;
	protected ServletContext context;
	protected int callType = 0;
	protected JSONObject ajax;

	public void setSession(Map map) {
		this.session = ((SessionMap) map);
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		String _callType = request.getHeader("call-type");
		if (_callType != null)
			this.callType = 1;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	protected String getParameter(String name) {
		return this.request.getParameter(name);
	}

	protected String getParameter(String name, String defaultValue) {
		String value = this.request.getParameter(name);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	protected short getParameter(String name, short defaultValue) {
		try {
			String value = this.request.getParameter(name);
			if (value == null) {
				return defaultValue;
			}
			return Short.parseShort(value);
		} catch (Exception e) {
		}
		return defaultValue;
	}

	protected int getParameter(String name, int defaultValue) {
		try {
			String value = this.request.getParameter(name);
			if (value == null) {
				return defaultValue;
			}
			return Integer.parseInt(value);
		} catch (Exception e) {
		}
		return defaultValue;
	}

	protected long getParameter(String name, long defaultValue) {
		try {
			String value = this.request.getParameter(name);
			if (value == null) {
				return defaultValue;
			}
			return Long.parseLong(value);
		} catch (Exception e) {
		}
		return defaultValue;
	}

	protected double getParameter(String name, double defaultValue) {
		try {
			String value = this.request.getParameter(name);
			if (value == null) {
				return defaultValue;
			}
			return Double.parseDouble(value);
		} catch (Exception e) {
		}
		return defaultValue;
	}

	protected String getParameterOfTab() {
		String t = getParameter("t", "");
		return t;
	}

	protected String getParameterOfTab(String defaultTab) {
		String t = getParameter("t", "");
		if (t.equals("")) {
			t = defaultTab;
		}
		return t;
	}

	protected String getParameterOfBreadcrumb() {
		String b = getParameter("b", "");
		return b;
	}

	protected String getParameterOfBreadcrumb(String defaultBreadcrumb) {
		String b = getParameter("b", "");
		if (b.equals("")) {
			b = defaultBreadcrumb;
		}
		return b;
	}

	protected int getParameterOfPage() {
		int p = getParameter("p", 1);
		return p;
	}

	protected String getParameterOfQueryType() {
		String qt = this.request.getParameter("qt");
		if ((qt != null) && (qt.equals(""))) {
			qt = null;
		}
		return qt;
	}

	protected String getParameterOfQuery() {
		String q = this.request.getParameter("q");
		if ((q != null) && (q.equals(""))) {
			q = null;
		}
		return q;
	}

	protected void setBeanByRequest(BaseEntity bean) throws Exception {
		Field[] fields = bean.getClass().getDeclaredFields();
		Field.setAccessible(fields, true);
		for (int i = 0; i < fields.length; ++i) {
			String fieldName = fields[i].getName();
			String fieldValue = this.request.getParameter(fieldName);
			if (fieldValue != null) {
				Class type = fields[i].getType();
				String typeName = type.getSimpleName();
				if (typeName.equals("long")) {
					fields[i].setLong(bean, getParameter(fieldName, 0L));
				} else if (typeName.equals("int")) {
					fields[i].setInt(bean, getParameter(fieldName, 0));
				} else if (typeName.equals("short")) {
					// fields[i].setShort(bean, getParameter(fieldName, 0));
				} else if (typeName.equals("double")) {
					fields[i].setDouble(bean, getParameter(fieldName, 0.0D));
				} else {
					fields[i].set(bean, getParameter(fieldName));
				}
			}
		}
	}

	protected void setOutput(String name, Object obj) {
		if (this.callType == 0) {
			this.request.setAttribute(name, obj);
		} else {
			if (this.ajax == null) {
				this.ajax = new JSONObject();
				this.ajax.put("doneCode", 0);
			}
			this.ajax.put(name, obj);
		}
	}

	protected void setOutput(BaseEntity entity) {
		if (this.callType == 0) {
			this.request.setAttribute("info", entity);
		} else if (this.ajax == null) {
			if (entity != null)
				this.ajax = JSONHelper.bean2JSONObject(entity);
			else {
				this.ajax = new JSONObject();
			}
			this.ajax.put("doneCode", 0);
		} else if (entity != null) {
			JSONObject jObj = JSONObject.fromBean(entity);
			JSONHelper.concat(this.ajax, jObj);
		}
	}

	protected void setReturnCode(int code) {
		if (this.ajax == null) {
			this.ajax = new JSONObject();
		}
		this.ajax.put("doneCode", code);
	}

	protected int getRecodeCode() {
		if (this.ajax == null) {
			return 0;
		}
		return this.ajax.getInt("doneCode");
	}

	protected void setOutputOfPagination(Pagination<?> pagination) {
		this.request.setAttribute("pagination", pagination);
	}

	protected void writeOutput() throws IOException {
		if (this.ajax == null) {
			this.ajax = new JSONObject();
			this.ajax.put("doneCode", 0);
		}
		this.response.setContentType("application/Json;charset=utf-8");
		this.response.getWriter().write(this.ajax.toString());
	}
}